package com.sscaipiao.android.activity.ThreeD;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SimpleAdapter;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.NormalPayActivity;
import com.sscaipiao.android.activity.DaLeTou.DaLeTouActivity;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SanDZuLiuHeZhiPayActivity extends NormalPayActivity {

	String[] mFrom = new String[]{"double_color_ball_pay_text3",
			"double_color_ball_pay_text4",
			"double_color_ball_add_1_or_x",
			"double_color_ball_add_howmuch"};

	int[] mTo = new int[]{
			R.id.double_color_ball_pay_text3,
			R.id.double_color_ball_pay_text4,
			R.id.double_color_ball_add_1_or_x,
			R.id.double_color_ball_add_howmuch
	};
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleText("3D直选");

		//setupOrderList();

		
	}

	private void setupOrderList() {
		mList = new ArrayList<Map<String,Object>>();
		Map<String,Object> mMap = null;
		chooseNumber = 0;
		for(int j = 0;j < choosen.size() ;j++){
			mMap = new HashMap<String,Object>();
			Log.v("choosen", choosen.get(j));
			String[] split = choosen.get(j).split(Constants.STRING_SPLITE_COMPLEX);
			mMap.put("double_color_ball_pay_text3", split[0].subSequence(0, 17));
			mMap.put("double_color_ball_pay_text4",  split[1].subSequence(0, 2));
			mMap.put("double_color_ball_add_1_or_x",  split[2]);
			mMap.put("double_color_ball_add_howmuch",  split[3]);
			chooseNumber = chooseNumber + Integer.parseInt(split[3].substring(0, split[3].indexOf("注")));
			mList.add(mMap);
		}
		SimpleAdapter mAdapter = new SimpleAdapter(this,mList,R.layout.double_color_ball_pay_item,mFrom,mTo);
		setListAdapter(mAdapter);
	}
	private String randomChoose() {
		Integer[] randomRedBalls = StringUtil.getRandom(33, 6);
		Integer[] randomBlueBalls = StringUtil.getRandom(16, 1);

		String redBalls = "";
		String blueBalls = "";
		String singleOrMult = "";

		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_add_1);
		String number = String.format(sAgeFormat1, "1");

		for (int num : randomRedBalls) {
			if (num < 10) {
				redBalls += ("0" + num + Constants.STRING_SPLITE);
			} else {
				redBalls += (num + Constants.STRING_SPLITE);
			}

		}
		for (int num : randomBlueBalls) {
			if (num < 10) {
				blueBalls += ("0" + num + Constants.STRING_SPLITE);
			} else {
				blueBalls += (num + Constants.STRING_SPLITE);
			}

		}

		singleOrMult = getResources().getString(R.string.double_color_ball_add_single);

		String str = redBalls.substring(0, redBalls.length() - 1) + Constants.STRING_SPLITE_COMPLEX +
				blueBalls.substring(0, blueBalls.length() - 1) + Constants.STRING_SPLITE_COMPLEX +
				singleOrMult + Constants.STRING_SPLITE_COMPLEX + number;
		return str;
	}

	private void addOrderByAuto() {
		if (choosen == null) {
			choosen = new ArrayList<String>();
		}

		String str = randomChoose();

		while (choosen.contains(str)) {
			str = randomChoose();
		}
		choosen.add(str);
		user.setChoosen(choosen);
		//showChoosen();

		recalculatorTotal();
	}
	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (R.id.add_by_hand == v.getId()) {
			Intent intent = new Intent(this, DaLeTouActivity.class);
			Bundle b = new Bundle();
			b.putSerializable(Constants.USER, user);
			b.putSerializable("date", date);
			b.putSerializable("leftTime", leftTime);
			intent.putExtras(b);
			startActivity(intent);
		} else if (R.id.add_by_auto == v.getId()) {
			addOrderByAuto();
		}
	    if ( R.id.pay_sure  == v.getId()){
			
//			if(chooseNumber <= 0) {
//				Toast.makeText(DaLeTouPayActivity.this, "请投注", Toast.LENGTH_SHORT).show();
//				return;
//			}else 			if( StringUtil.isEmptyString(double_color_ball_pay_add_date.getText().toString()) ||
//			Integer.parseInt(double_color_ball_pay_add_date.getText().toString()) < 1 ||
//			Integer.parseInt(double_color_ball_pay_add_date.getText().toString()) > 14) {
//				Toast.makeText(DaLeTouPayActivity.this, "请选择正数期数(1 - 14)", Toast.LENGTH_SHORT).show();
//				return;
//			}else 			if( StringUtil.isEmptyString(double_color_ball_pay_add_number.getText().toString()) ||
//					Integer.parseInt(double_color_ball_pay_add_number.getText().toString()) < 1) {
//				Toast.makeText(DaLeTouPayActivity.this, "请选择正数倍数", Toast.LENGTH_SHORT).show();
//				return;
//			}else 			if( ! double_color_ball_pay_agree.isChecked()) {
//				Toast.makeText(DaLeTouPayActivity.this, "请阅读并确认 电话购彩协议", Toast.LENGTH_SHORT).show();
//				return;
//			}
//
//			String retStr = CaipiaoServiceHelper.payLotteryDoubleColorBall(DaLeTouPayActivity.this,
//					user, date,
//					//double_color_ball_pay_add_date.getText().toString(),
//					double_color_ball_pay_add_number.getText().toString(),
//					totalMoney()/Integer.parseInt(double_color_ball_pay_add_date.getText().toString()) + "");
//			String zuihao = Constants.LOGIN_SUCCESS_CODE;
//			// 追号 大于 一期 才调用追号接口
//					if((Integer.parseInt(double_color_ball_pay_add_date.getText().toString()) ) > 1){
//						zuihao = CaipiaoServiceHelper.betZHSSQ(DaLeTouPayActivity.this,
//								user, date, double_color_ball_pay_add_number.getText().toString(),
//								totalMoney()* ((Integer.parseInt(double_color_ball_pay_add_date.getText().toString()) ) - 1)/
//								(Integer.parseInt(double_color_ball_pay_add_date.getText().toString()) )+ "", null, null);
//					}
//
//			if(retStr.startsWith(Constants.LOGIN_SUCCESS_CODE) && zuihao.startsWith(Constants.LOGIN_SUCCESS_CODE)){
//				Toast.makeText(DaLeTouPayActivity.this, "购买成功", Toast.LENGTH_SHORT).show();
//				Intent intent = new Intent( DaLeTouPayActivity.this,HallActivity.class);
//				Bundle b = new Bundle();
//				b.putSerializable(Constants.USER, user);
//				intent.putExtras(b);
//				startActivity(intent);
//
//			} else {
//				Toast.makeText(DaLeTouPayActivity.this, retStr.split(Constants.STRING_SPLITE_COMPLEX)[1], Toast.LENGTH_SHORT).show();
//				return;
//			}
			
		} else if (R.id.pay_together  == v.getId() ){
//			if(Integer.parseInt(double_color_ball_pay_add_date.getText().toString()) > 1) {
//				Toast.makeText(DaLeTouPayActivity.this, "追号不可以发合买", Toast.LENGTH_SHORT).show();
//				return;
//			} else {
//				Intent intent = new Intent( DaLeTouPayActivity.this,StartTogetherActivity.class);
//				Bundle b = new Bundle();
//				b.putSerializable(Constants.USER, user);
//				b.putSerializable("date", date);
//				b.putSerializable("totalMoney", totalMoney());
//				b.putSerializable("schemeNum1", CaipiaoServiceHelper.combineSchemeNum1(user,
//						double_color_ball_pay_add_number.getText().toString(), totalMoney()+""));
//				intent.putExtras(b);
//				startActivity(intent);
//			}

		}

	}



}
